﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BGSpaceShooter
{
    public class ObserverManager
    {
        private List<IStateObserver> StateObservers { get; set; }
        private List<IStatusObserver> StatusObservers { get; set; }

        public ObserverManager()
        {
            StatusObservers = new List<IStatusObserver>();
            StateObservers = new List<IStateObserver>();
        }

        public void Register(IStateObserver stateObserver)
        {
            StateObservers.Add(stateObserver);
        }

        public void Register(IStatusObserver statusObserver)
        {
            StatusObservers.Add(statusObserver);
        }

        public void Notify(IEnumerable<ServerStatusResponse> serverStatusResponses)
        {
            foreach (IStatusObserver statusObserver in StatusObservers)
            {
                lock (statusObserver)
                {
                    if (serverStatusResponses.Count() > 0)
                    {
                        statusObserver.BeginUpdate();
                        foreach (ServerStatusResponse serverStatusResponse in serverStatusResponses)
                        {
                            statusObserver.Update(serverStatusResponse);
                        }
                        statusObserver.EndUpdate();
                    }
                }
            }
        }


        public void Notify(IEnumerable<ServerStateResponse> serverStateResponses)
        {
            foreach (ServerStateResponse serverStateResponse in serverStateResponses)
            {
                foreach (IStateObserver stateObserver in StateObservers)
                {
                    stateObserver.Update(serverStateResponse);
                }
            }
        }
    }
}
